using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using CoachingOrganizer.iOS.Controllers.Tools.MoodLevel;
using CoachingOrganizer.Logic.Models.Tools.MoodLevel;
using CoachingOrganizer.iOS.Decorators;
using CoachingOrganizer.iOS.Helpers;

namespace CoachingOrganizer.iOS.DataSources.Tools.MoodLevel
{
    public class MoodValuesDataSource : UITableViewSource
    {
        public const string CellIdentifer = "MoodValueCell";

        private readonly UITableView _view;
        private readonly MoodLevelInputDetailsViewController _controller;
        private readonly MoodLevelModel _model;
        private readonly SimpleTableDataSourceDecorator<MoodValueViewController, MoodValueCacheModel> _tableCellsSource;

        public MoodValuesDataSource(UITableView view, MoodLevelInputDetailsViewController controller, MoodLevelModel model)
        {
            _view = view;
            _controller = controller;
            _model = model;
            _tableCellsSource = new SimpleTableDataSourceDecorator<MoodValueViewController, MoodValueCacheModel>(CellIdentifer, _view, _controller, p => GetRowModel(p), m => new MoodValueViewController(m));
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            return _tableCellsSource.GetCell(indexPath);
        }

        public override int RowsInSection(UITableView tableview, int section)
        {
            MoodDayCacheModel dayModel = _model.SelectedDay;
            return dayModel == null ? 0 : UIHelper.GetCollectionItemsCount(dayModel.Values);
        }

        private MoodValueCacheModel GetRowModel(NSIndexPath p)
        {
            MoodDayCacheModel dayModel = _model.SelectedDay;
            return dayModel == null ? null : dayModel.Values.ElementAtOrDefault(p.Row);
        }
    }
}